<!DOCTYPE html>
<html>
<head>
  <meta charset="UTF-8">
  <!-- import CSS -->
  <link rel="stylesheet" href="https://unpkg.com/element-ui/lib/theme-chalk/index.css">
</head>
<body>
<div id="app">
  <el-page-header
          style="background-color: #0096dc;color: white;line-height: 60px"
          @back="goBack()" content="添加轮播图页面"></el-page-header>
  <!--上传组件开始-->
  <el-upload
          action="/upload"
          name="picFile"
          limit="1"
          list-type="picture-card"
          :on-success="handleSuccess"
          :on-preview="handlePictureCardPreview"
          :on-remove="handleRemove">
    <i class="el-icon-plus"></i>
  </el-upload>
  <el-dialog :visible.sync="dialogVisible">
    <img width="100%" :src="dialogImageUrl" alt="">
  </el-dialog>
  <!--上传组件结束-->
  <el-button type="success" @click="insert">开始上传</el-button>
</div>
</body>
<!-- import Vue before Element -->
<script src="https://cdn.bootcdn.net/ajax/libs/axios/0.21.1/axios.min.js"></script>
<script src="https://unpkg.com/vue@2/dist/vue.js"></script>
<!-- import JavaScript -->
<script src="https://unpkg.com/element-ui/lib/index.js"></script>
<script>
  let v = new Vue({
    el: '#app',
    data: function() {
      return {
        dialogImageUrl: '',
        dialogVisible: false,
        url:''
      }
    },
    methods:{
      goBack(){
        history.back();//返回上一页面
      },
      insert(){
        if(v.url==""){
          v.$message.error("请选择上传的图片!");
          return;
        }
        //发出异步请求添加轮播图
        axios.post("/banner/insert?url="+v.url).then(function () {
            location.href="/admin.html";//添加成功后跳转到管理页面
        })
      },
      handleSuccess(response,file,fileList){
         v.url = response;
      },
      handleRemove(file, fileList) {
         v.url = "";//删除上传图片的路径
          //发起异步请求，删除图片
          axios.post("remove?url="+file.response).then(function () {
              v.$message.success("删除图片成功");
          })
      },
      handlePictureCardPreview(file) {
        this.dialogImageUrl = file.url;
        this.dialogVisible = true;
      }
    }
  })
</script>
</html>